﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Messaging;

namespace WinFormTest
{
    public partial class Form1 : System.Windows.Forms.Form
    {
        public Form1()
        {
            InitializeComponent();
            textBox1.Text = @"FormatName:Direct=TCP:192.168.0.223\private$\myQueue";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string mqPath = textBox1.Text;
            MessageQueue mq = new MessageQueue(mqPath);
            Message ms = new Message();
            ms.Label = "测试时间：" + DateTime.Now.ToShortDateString();
            //ms.Formatter = new XmlMessageFormatter(new string[] { "System.String" });
            ms.Body = textBox2.Text;
            ms.BodyType = int.Parse(textBox3.Text);
            ms.Recoverable = true;
            //ms.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
            mq.Send(ms);
            //mq.ReceiveByCorrelationId("").
            //mq.Receive();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                string mqPath = textBox1.Text;
                MessageQueue mq = new MessageQueue(mqPath);
                mq.Formatter = new XmlMessageFormatter(new string[] { "System.String" });
                Message ms = mq.Receive();
                label1.Text = "Body:" + (string)ms.Body + "\r\nType:" + ms.BodyType.ToString();

                MessageQueue mq2 = new MessageQueue(@"FormatName:Direct=TCP:192.168.0.223\private$\FailMessage");
                mq2.Send(ms);
            }
            catch (Exception ex)
            {
                label1.Text = ex.Message;
            }
        }
    }
}
